﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DataManagement : System.Web.UI.Page
{
    private static string strConn = ConfigurationManager.ConnectionStrings["LabConnectionString"].ConnectionString;

    private const string MENU = "ManageMenu";
    private const string USER = "ManageUser";
    private const string LAB = "ManageLab";
    private const string VENDOR = "ManageVendor";
    


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Count > 0)
        {
            string toManage = Request.QueryString[0];
            switch (toManage)
            {
                case MENU:
                    processMenu();
                    break;
                case USER:
                    processUser();
                    break;
                case LAB:
                    processLab();
                    break;
                case VENDOR:
                    processVendor();
                    break;
                default:
                    break;
            }
        }

        //Response.ContentType = "application/json";
        //var serializer = new JavaScriptSerializer();
        //string json = serializer.Serialize(Request.Form);
        //Response.Write(json);
    }

    private void processMenu()
    {
        DatabaseResponse result = new DatabaseResponse();
        try
        {
            //using (SqlConnection cn = new SqlConnection(strConn))
            //{
            //    cn.Open();
            //    string query = "sp_barang_keluar {0}, {1}, {2}, {3}, '{4}', {5}";
            //    using (SqlCommand cmd = new SqlCommand(string.Format(query, barang, tahun, jumlah, lab, keterangan, 2)))
            //    {
            //        cmd.Connection = cn;
            //        int ra = cmd.ExecuteNonQuery();
            //        if (ra > 0)
            //        {
            //            result.Status = true;
            //        }
            //        else
            //        {
            //            result.Status = false;
            //            result.ErrorMessage = "Tidak ada data yang berhasil ditulis.";
            //        }
            //    }
            //    cn.Close();
            //}
        }
        catch (Exception ex)
        {

            result.Status = false;
            result.ErrorMessage = ex.Message;
        }
        //return result;

        Response.ContentType = "application/json";
        var serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(result);
        Response.Write(json);
    }

    private void processUser()
    {
        string action = Request.Form["action"];
        string id = Request.Form["id"];

        string query = string.Empty;
        if (action.Equals("edit"))
        {
            string nama = Request.Form["nama"];
            string password = Request.Form["password"];
            query = "update MST_USER SET PASSWORD='" + password + "', NAMA='" +nama + "' WHERE ID=" + id;
        }
        else if (action.Equals("delete"))
        {
            query = "update MST_USER SET ACTIVE=0 WHERE ID=" + id;
        }

        Response.ContentType = "application/json";
        var serializer = new JavaScriptSerializer();
        Response.Write(serializer.Serialize(execQuery(query)));
    }

    private DatabaseResponse execQuery(string query)
    {
        DatabaseResponse result = new DatabaseResponse();
        try
        {
            using (SqlConnection cn = new SqlConnection(strConn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = cn;
                    int ra = cmd.ExecuteNonQuery();
                    if (ra > 0)
                    {
                        result.Status = true;
                    }
                    else
                    {
                        result.Status = false;
                        result.ErrorMessage = "Tidak ada data yang berhasil ditulis.";
                    }
                }
                cn.Close();
            }
        }
        catch (Exception ex)
        {
            result.Status = false;
            result.ErrorMessage = ex.Message;
        }
        return result;
    }

    private void processLab()
    {
        string action = Request.Form["action"];
        string id = Request.Form["id"];

        string query = string.Empty;
        if (action.Equals("edit"))
        {
            string nama = Request.Form["nama"];
            query = "UPDATE MST_LAB set NAMA_LAB='" + nama + "' WHERE ID=" + id;
        }
        else if (action.Equals("delete"))
        {
            query = "DELETE FROM MST_LAB WHERE ID=" + id;
        }

        Response.ContentType = "application/json";
        var serializer = new JavaScriptSerializer();
        Response.Write(serializer.Serialize(execQuery(query)));
    }

    private void processVendor()
    {
        string action = Request.Form["action"];
        string id = Request.Form["id"];

        string query = string.Empty;
        if (action.Equals("edit"))
        {
            string nama = Request.Form["nama"];
            query = "UPDATE MST_LAB set NAMA_LAB='" + nama + "' WHERE ID=" + id;
        }
        else if (action.Equals("delete"))
        {
            query = "DELETE FROM MST_LAB WHERE ID=" + id;
        }

        Response.ContentType = "application/json";
        var serializer = new JavaScriptSerializer();
        Response.Write(serializer.Serialize(execQuery(query)));
    }

    public class DatabaseResponse
    {
        public bool Status;
        public string ErrorMessage;
        public string Value;
    }
}